package oa.hleast.xswl.controller.admin.place;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.pojo.form.place.CmsPlaceTypeForm;
import oa.hleast.xswl.pojo.form.place.PlaceInfoAttributesForm;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceInfoAttributesVo;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceTypeVo;
import oa.hleast.xswl.pojo.vo.place.ParentPlaceTypeVo;
import oa.hleast.xswl.service.place.IPlaceInfoAttributesService;
import oa.hleast.xswl.service.place.IPlaceTypeService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;

/**
 * @author 302
 * @description
 * @date 2021/3/10
 */
@Api(tags = "【系统管理】地图分类管理")
@RestController("PlaceTypeController")
@RequestMapping("/api.admin/v1/place-type")
@AllArgsConstructor
@Slf4j
public class PlaceTypeController {

    private final IPlaceTypeService placeTypeService;
    private final IPlaceInfoAttributesService placeInfoAttributesService;


    @ApiOperation(value = "新增地图分类", httpMethod = "POST")
    @ApiImplicitParam(name = "placeTypeForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsPlaceTypeForm",dataTypeClass = CmsPlaceTypeForm.class)
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody @Valid CmsPlaceTypeForm placeTypeForm) {
        Long id = placeTypeService.add(placeTypeForm);
        return Result.judge(ObjectUtils.isNotEmpty(id));
    }

    @ApiOperation(value = "修改地图分类", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "placeTypeForm", value = "实体JSON对象", required = true, paramType = "body", dataType = "CmsPlaceTypeForm",dataTypeClass = CmsPlaceTypeForm.class)
    })
    @PutMapping(value = "update/{id}")
    public Result<Boolean> update(
            @PathVariable @NonNull Integer id,
            @RequestBody @NonNull CmsPlaceTypeForm placeTypeForm) {

        if ((int) id != placeTypeForm.getId()) {
            throw new BizException("数据ID不一致");
        }
        placeTypeService.update(placeTypeForm);
        return Result.judge(true);
    }

    @ApiOperation(value = "地图分类列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 page分页查询", required = true, paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "typeName", value = "分类名称", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    })
    @GetMapping("query-list")
    public Result<List<CmsPlaceTypeVo>> list(
            @RequestParam @NotBlank String queryMode,
            @RequestParam @NonNull Integer page,
            @RequestParam @NonNull Integer limit,
            @RequestParam @NonNull String typeName) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        Page<CmsPlaceTypeVo> result = null;
        switch (queryModeEnum) {
            case PAGE:
                result = placeTypeService.pageList(page, limit, typeName, null, null);
                return Result.success(result.getRecords(), result.getTotal());
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "地图分类列表分层级", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式 tree树形查询 / cascader 级联查询", required = true, paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "typeName", value = "分类名称", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    })
    @GetMapping("find-level-list")
    public Result<List<CmsPlaceTypeVo>> list(
            @RequestParam @NotBlank String queryMode,
            @RequestParam @NonNull String typeName) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        List<CmsPlaceTypeVo> result = null;
        switch (queryModeEnum) {
            case TREE:
                return Result.success(null);
            case CASCADER:
                result = placeTypeService.cascaderPlaceTypeList(typeName);
                return Result.success(result);
            default:
                throw new BizException("查询模式错误，请联系管理员");
        }
    }

    @ApiOperation(value = "地图分类详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "资讯id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("get-details/{id}")
    public Result<CmsPlaceTypeVo> detail(@PathVariable Long id) {
        CmsPlaceTypeVo placeTypeVo = placeTypeService.detail(id);
        return Result.success(placeTypeVo);
    }

    @ApiOperation(value = "删除地图分类", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String",dataTypeClass = String.class)
    @DeleteMapping("delete/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") String ids) {
        boolean status = placeTypeService.deleteByIds(Arrays.asList(ids.split(",")));
        return Result.judge(status);
    }

    @ApiOperation(value = "地图父级分类列表", httpMethod = "GET")
    @GetMapping("getParentPlaceType")
    public Result<List<ParentPlaceTypeVo>> getParent() {
        List<ParentPlaceTypeVo> parentPlaceTypeVo = placeTypeService.getParent();
        return Result.success(parentPlaceTypeVo);
    }

    @ApiOperation(value = "获取场所类型的附加属性列表", httpMethod = "GET")
    @GetMapping("getAttributesList")
    public Result<List<CmsPlaceInfoAttributesVo>> getAttributes(@RequestParam Long placeTypeId) {
        List<CmsPlaceInfoAttributesVo> voList = placeInfoAttributesService.getByPlaceId(placeTypeId);
        return Result.success(voList);
    }

    @ApiOperation(value = "保存场所信息的附加属性", httpMethod = "POST")
    @PostMapping("saveAttributesList")
    public Result<Boolean> saveAttributesList(@RequestBody @Valid List<PlaceInfoAttributesForm> formList) {
        if (CollectionUtil.isEmpty(formList)) {
            return Result.failed("至少提交一条规格");
        }

        Boolean result = placeInfoAttributesService.saveAttributesList(formList);
        return Result.judge(result);
    }

}
